package com.ruoyi.system.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.reservationserver.dao.StadiumDao;
import com.ruoyi.reservationserver.service.StadiumService;
import com.ruoyi.reservationserver.service.StadiumStatisticService;
import com.ruoyi.reservationserver.utils.Result;
import com.ruoyi.system.domain.StadiumStatistic;
import com.ruoyi.system.mapper.StadiumStatisticMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Stadium;
import com.ruoyi.system.service.IStadiumService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 球馆管理Controller
 * 
 * @author guiye
 * @date 2023-08-13
 */
@RestController
@RequestMapping("/system/stadium")
public class StadiumController extends BaseController
{
    @Autowired
    private IStadiumService stadiumService;

    /**
     * 服务对象
     */
    @Resource
    private StadiumService stadiumService1;

    @Resource
    private StadiumDao stadiumDao;

    @Resource
    private StadiumStatisticService stadiumStatisticService;


    /**
     * 查询球馆管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:stadium:list')")
    @GetMapping("/list")
    public TableDataInfo list(Stadium stadium)
    {
        startPage();
        List<Stadium> list = stadiumService.selectStadiumList(stadium);
        return getDataTable(list);
    }

    /**
     * 导出球馆管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:stadium:export')")
    @Log(title = "球馆管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Stadium stadium)
    {
        List<Stadium> list = stadiumService.selectStadiumList(stadium);
        ExcelUtil<Stadium> util = new ExcelUtil<Stadium>(Stadium.class);
        util.exportExcel(response, list, "球馆管理数据");
    }

    /**
     * 获取球馆管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:stadium:query')")
    @GetMapping(value = "/{stadiumId}")
    public AjaxResult getInfo(@PathVariable("stadiumId") Long stadiumId)
    {
        return success(stadiumService.selectStadiumByStadiumId(stadiumId));
    }

    /**
     * 新增球馆管理
     */
    @PreAuthorize("@ss.hasPermi('system:stadium:add')")
    @Log(title = "球馆管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Stadium stadium)
    {
        QueryWrapper<StadiumStatistic> wrapper = new QueryWrapper<>();
        wrapper.eq("stadium_statistic_id",1);
        StadiumStatistic stadiumStatistic = stadiumStatisticService.getOne(wrapper);
        stadiumStatistic.setTotalStadiumNum(stadiumService1.count()+1);
        stadiumStatisticService.update(stadiumStatistic);
        return toAjax(stadiumService.insertStadium(stadium));
    }

    /**
     * 修改球馆管理
     */
    @PreAuthorize("@ss.hasPermi('system:stadium:edit')")
    @Log(title = "球馆管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Stadium stadium)
    {
        return toAjax(stadiumService.updateStadium(stadium));
    }

    /**
     * 删除球馆管理
     */
    @PreAuthorize("@ss.hasPermi('system:stadium:remove')")
    @Log(title = "球馆管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{stadiumIds}")
    public AjaxResult remove(@PathVariable Long[] stadiumIds)
    {
        QueryWrapper<StadiumStatistic> wrapper = new QueryWrapper<>();
        wrapper.eq("stadium_statistic_id",1);
        StadiumStatistic stadiumStatistic = stadiumStatisticService.getOne(wrapper);
        stadiumStatistic.setTotalStadiumNum(stadiumService1.count()-1);
        stadiumStatisticService.update(stadiumStatistic);
        return toAjax(stadiumService.deleteStadiumByStadiumIds(stadiumIds));
    }


    /**
     * 获取分页球馆信息
     * @param page
     * @param count
     * @return
     */
    @GetMapping
    public Result index(@RequestParam(value = "page", defaultValue = "1") int page,
                        @RequestParam(value = "count", defaultValue = "10") int count) {
        PageInfo<Stadium> pageInfo = stadiumService1.selectStadiumWithPage(page, count);
        return Result.success(pageInfo);
    }

    /**
     * 通过球馆名称查询球馆信息
     * @param stadiumName
     * @return
     */
    @GetMapping("selectStadiumListByStadiumName")
    public Result selectStadiumListByStadiumName(@RequestParam String stadiumName){
        List<Stadium> stadiumList;
        if (stadiumName==null){
            return Result.error("不能输入空信息");
        }
        stadiumList = stadiumDao.selectStadiumListByCourtName(stadiumName);
        return Result.success(stadiumList);
    }

    /**
     * 新增数据
     *
     * @param stadium 实体
     * @return 新增结果
     */
    @PostMapping("add")
    public Result add1(@RequestBody Stadium stadium) {
        System.out.println("球馆："+stadium);
        this.stadiumService1.insert(stadium);

        QueryWrapper<StadiumStatistic> wrapper = new QueryWrapper<>();
        wrapper.eq("stadium_statistic_id",1);
        StadiumStatistic stadiumStatistic = stadiumStatisticService.getOne(wrapper);
        stadiumStatistic.setTotalStadiumNum(stadiumService1.count());
        stadiumStatisticService.update(stadiumStatistic);
        return Result.success();
    }

    /**
     * 编辑数据
     *
     * @param stadium 实体
     * @return 编辑结果
     */
    @PostMapping("/edit")
    public Result edit1(@RequestBody Stadium stadium) {
        try {
            System.out.println(stadium);
            if (stadium==null){
                return Result.error("数据不能为空");
            }
            System.out.println(stadium.getStadiumId());
            if(stadium.getStadiumId()==null){
                return Result.error("id不能为空");
            }
            this.stadiumService1.update(stadium);
            return Result.success();

        }catch (Exception e){
            return Result.error("更新失败");
        }

    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping("/del")
    public Result deleteById(@RequestParam("stadiumId") Integer id) {
        if(this.stadiumService1.deleteById(id)){
            QueryWrapper<StadiumStatistic> wrapper = new QueryWrapper<>();
            wrapper.eq("stadium_statistic_id",1);
            StadiumStatistic stadiumStatistic = stadiumStatisticService.getOne(wrapper);
            stadiumStatistic.setTotalStadiumNum(stadiumService1.count());
            stadiumStatisticService.update(stadiumStatistic);
            return Result.success();
        }else {
            return Result.error("id不存在");
        }

    }
}
